import { complaint,complaintList,complaintUndo } from '@/pages/api/index.js'
export default {
    data(){
      return{
          options: [{
            value: '0',
            label: '待处理'
          }, {
            value: '-1',
            label: '已撤销'
          },{
          value: '1',
          label: '已完成'
          }, {
          value: '2',
          label: '已拒接'
        }],
          dataList:[],
           params:{
             order:'',
             state:"",
             pageIndex:1,
             pageSize:10
           },
           form: {
              order: '',       //订单编号
              type: '',        //任务类型
              content: '',     //投诉说明
              imgs:'',         //图片id集合
            },
            isDisabled:false,
        formLabelWidth: '120px',
        dialogFormVisible: false,
        textarea3: '',
        fileList2: [],         //上传图片展示的一个对象
        succList:[],             //上传成功图片的集合
        removeList:[],
        count:0,
        time:[],               //选择日期，由数组表示开始时间已经结束时间
        rules:{
          order: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
          ],
          type: [
            { required: true, message: '请选择活动区域', trigger: 'change' }
          ],
          content: [
            { required: true, message: '请输入投诉说明', trigger: 'blur' }
          ],
        },
        imgData:{
          //  accept: 'image/gif, image/bmp, image/png, image/jpg',
           accept:['.gif','.bmp','.png','.jpg']
         }
      }
    },
    methods:{
      /* 文件上传成功时的钩子 */
      handleSuccess(response){
        this.succList.push(response.data.id)
      },
      /* 文件移除时的钩子 */
      handleRemove(file, fileList) {
        this.removeList.push(file.response.data.id)
      },
      //限制图片格式 accept="image/gif,image/jpg,image/png,image/bmp"
      beforeAvatarUpload(file) {
        var fType = file.name.substr(file.name.lastIndexOf("."))
          let type = file.type
          if(this.imgData.accept.indexOf(fType) == -1){
              this.$message.warning('不能使用除了gif，bmp，png，jpg之外的格式')
              return false
          }
      },
      /* 提交申诉 */
      submit(formName){
        this.$refs[formName].validate(valid=>{
           if(valid){
            var arr1 = this.succList //数组A
            var arr2 = this.removeList//数组B
            var temp = []; //临时数组1
            var temparray = [];//临时数组2
            for (var i = 0; i < arr2.length; i++) {
            temp[arr2[i]] = true;//巧妙地方：把数组B的值当成临时数组1的键并赋值为真
            };
            for (var i = 0; i < arr1.length; i++) {
            if (!temp[arr1[i]]) {
            temparray.push(arr1[i]);//巧妙地方：同时把数组A的值当成临时数组1的键并判断是否为真，如果不为真说明没重复，就合并到一个新数组里，这样就可以得到一个全新并无重复的数组
            } ;
            };
            this.form.imgs = temparray.join()
            if(this.form.imgs == ''){
                 this.$message.error('请上传图片')
                 return
            }else{
              complaint(this.form).then(res=>{
                    if(res.code == 0){
                        this.init()
                        this.$message.success('已提交')
                        this.dialogFormVisible = false
                    }else{
                         this.$message.error(res.msg)
                    }
              })
            }
           }else{
               return
           }
        })
      },
      //初始化
      init(){
        complaintList(this.params).then(res=>{
              res.data.complaint.forEach(item => {
                  if(item.state == -1){
                     item.state = '已撤销'
                  }else if(item.state == 0){
                     item.state = '撤销申诉'
                  }else if(item.state == 1){
                      item.state = '已完成'
                  }else {
                     item.state = '已拒接'
                  }
              })
              this.dataList = res.data.complaint
              this.count = res.data.count
        })
      },
      handleEdit(row){
         console.log(row,'row')
         row.state = '已撤销'
            complaintUndo({complaintId:row.id}).then(res=>{
                console.log(res,'res')
                if(res.code == 0){
                    this.$message.success('撤销成功')
                }else{
                    this.$message.warning(res.msg)
                }
            })
      },
      //查询
      search(){
        var reg = /^\s*$/g
        if(reg.test(this.params.order)){
             this.$message.warning('请输入订单号')
             return false
        }
        if(reg.test(this.params.state)){
             this.$message.warning('请选择申诉状态')
             return false
        }
        this.init()
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.params.pageSize = val
        this.init()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.params.pageIndex = val
        this.init()
      }
    },
    created(){
        this.init()
    }
}
